<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Repositories\Eloquent\MenuRepository;
use App\Http\Requests\MenuRequest;

class MenuController extends Controller
{
    public function __construct(MenuRepository $menu)
    {
        $this->menu = $menu;
    }

    /**
     * @return string
     */
    public function index()
    {
        $menu = $this->menu->findByField('parent_id', 0);
        $menus=$this->menu->sortMenu($this->menu->all()->toArray());

        dd($this->menu->sortMenuSetCache($menus));

        return view('admin.menu.list')->with(compact('menu'));
    }

    public function store(MenuRequest $request)
    {
        $result = $this->menu->create($request->all());
        if ($result) {
            flash('添加菜单成功', 'success');
        } else {
            flash('添加菜单失败', 'error');
        }

        return redirect('admin/menu');
    }
}
